﻿namespace MvcXRepairAdmin.Controllers
{
    using System.Collections.Generic;
    using System.Web.Mvc;
    using MvcXRepairAdmin.Interfaces;
    using MvcXRepairAdmin.Services;
    using XRepair.Models;
    using System.Linq;
    using XRepair.WebCore.Authentication;
    using XRepair.Conventions.Enum;

    [xRepairAdminAuthorize(RoleID = UserPermission.Admin)]
    public class ServiceController : BaseController
    {
        private IServiceService _serviceService;

        public ServiceController()
        {
            _serviceService = new ServiceService();
        }

        public ActionResult Index()
        {
            IEnumerable<Service> services = _serviceService.GetAllActiveServices();
            //List<Service> model = services.ToList();
            List<Service> model = services != null ? services.ToList() : new List<Service>();
            return View(model);
        }

        public ActionResult Delete(int id)
        {
            bool succeed = _serviceService.DeactivateServiceByID(id);

            if (succeed)
            {
                return RedirectToAction("Index");
            }

            return RedirectToAction("ErrorOnProcessing", "Error");
        }

        public ActionResult AddNew(Service model)
        {
            bool succeed = _serviceService.AddNewService(model);

            if (succeed)
            {
                return RedirectToAction("Index");
            }

            return RedirectToAction("ErrorOnProcessing", "Error");
        }

        public ActionResult Edit(Service model)
        {
            model.IsActive = true;
            bool succeed = _serviceService.EditService(model);

            if (succeed)
            {
                return RedirectToAction("Index");
            }

            return RedirectToAction("ErrorOnProcessing", "Error");
        }

    }
}
